/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package commands;
import java.util.*;
import IA.*;
//finds the first party (waiting longest) that can fit into the available table
//assumes there is only one available table
//this will be called as soon as one table is free
public class CmdCallNextWaitingParty extends Command {
    public CmdCallNextWaitingParty(InformationAggregator ia) {
        super(ia);
    }

    @Override
    public Object execute() {
        CustomerQueue cq = super.ia.getCustomerQueue();
        //loop through available tables
        //i wrote this so we can choose based on table sizes
        for(Iterator<Table> TableIterator = super.ia.getTableCollection().getAvailTableIterator(); TableIterator.hasNext(); ) {
            Table t = TableIterator.next();
            //loop through waiting parties, longest first
            for(Iterator<WaitingParty> PartyIterator = super.ia.getCustomerQueue().getLongestFirstIterator(); PartyIterator.hasNext(); ) {
                WaitingParty p = PartyIterator.next();
                //if party fits into table, call them and create a service for them
                if(t.getNumSeats() >= p.getNumCustomers()) {
                    //removes from queue and removes party from systemclock notify list
                    super.ia.getCustomerQueue().CallParty(p);
                    new CmdStartNewService(super.ia,p.getNumCustomers(),t.getID(),p.getEatTime()).execute();
                    //exit, return true indicating new service
                    return ia.getServiceCollection().getLastAddedService();
                }
            }
        }
        //if no table fit the waiting customers, return nothing as well (although nothing will have been done)
        return null;
    }
}
